package data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class POSGuestTable extends AbstractData{

	private long id = -1;
	private List<POSTable>	table;
	private String tableString;
	private POSGuest guest;
	private String guestString;
	private	String guestAmount;
	private	Date arrive;
	private Date nightAuditDate;
	private Date back;
	private boolean status;
	private List<POSOrder> POSorderList;
	private String POSorderListString;
	private String note;
	private boolean isClosed;
	private String guestStatus;
	
	private long hotelId;
	private long displayFolioId;

	private List<POSHistory> POSHistoryList;
	private String POSHistoryListString;
	
	public POSGuestTable()
	{
		status = true;
	}
	
	public POSGuestTable(long id){
		this.id = id;
		this.status = true;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public List<POSTable> getTable() {
		if ((table == null||table.size()== 0) && !(tableString == null || tableString.isEmpty())) {
			String[] idOfList = SplitList(tableString);
			List<POSTable> list = new ArrayList<POSTable>();
			if(idOfList.length == 1 && idOfList[0].equals("")){
				table = null;
			}else{
				for (String id : idOfList) {
					POSTable a = new POSTable(Long.parseLong(id));
					list.add(a);
				}
				for(POSTable a : list)
					a.LoadData();
				table = list;			
			}
		}
		return table;
	}

	public void setTable(List<POSTable> table) {
		this.tableString =CombineList(table);
		this.table = table;
	}

	public String getTableString() {
		return tableString;
	}

	public void setTableString(String tableString) {
		this.tableString = tableString;
	}

	public POSGuest getGuest() {
		if (guest == null && !(guestString == null || guestString.isEmpty())) {
			POSGuest c = new POSGuest(Long.parseLong(guestString));	
			c.LoadData();
			guest = c;
		}
		return guest;
	}

	public void setGuest(POSGuest guest) {
		this.guestString = String.format("%d", guest.getId());
		this.guest = guest;
	}

	public String getGuestString() {
		return guestString;
	}

	public void setGuestString(String guestString) {
		this.guestString = guestString;
	}

	public String getGuestAmount() {
		return guestAmount;
	}

	public void setGuestAmount(String guestAmount) {
		this.guestAmount = guestAmount;
	}

	public Date getArrive() {
		return arrive;
	}

	public void setArrive(Date arrive) {
		this.arrive = arrive;
	}

	public Date getNightAuditDate() {
		return nightAuditDate;
	}

	public void setNightAuditDate(Date nightAuditDate) {
		this.nightAuditDate = nightAuditDate;
	}

	public Date getBack() {
		return back;
	}

	public void setBack(Date back) {
		this.back = back;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}
	
	public List<POSOrder> getPOSorderList() {
		if ((POSorderList == null||POSorderList.size()== 0) && !(POSorderListString == null || POSorderListString.isEmpty())) {
			String[] idOfList = SplitList(POSorderListString);
			List<POSOrder> list = new ArrayList<POSOrder>();
			if(idOfList.length == 1 && idOfList[0].equals("")){
				POSorderList = null;
			}else{
				for (String id : idOfList) {
					POSOrder a = new POSOrder(Long.parseLong(id));
					list.add(a);
				}
				for(POSOrder a : list)
					a.LoadData();
				POSorderList = list;			
			}
		}
		return POSorderList;
	}

	public void setPOSorderList(List<POSOrder> pOSorderList) {
		this.POSorderListString =CombineList(pOSorderList);
		POSorderList = pOSorderList;
	}

	public String getPOSorderListString() {
		return POSorderListString;
	}

	public void setPOSorderListString(String pOSorderListString) {
		POSorderListString = pOSorderListString;
	}
	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public boolean isClosed() {
		return isClosed;
	}

	public void setClosed(boolean isClosed) {
		this.isClosed = isClosed;
	}
	
	public String getGuestStatus() {
		return guestStatus;
	}

	public void setGuestStatus(String guestStatus) {
		this.guestStatus = guestStatus;
	}
	
	
	public List<POSHistory> getPOSLastHistoryList(long itemCount){
		if (!(POSHistoryListString == null || POSHistoryListString.isEmpty())) {
			String[] idOfList = SplitList(POSHistoryListString);
			//long startIndex = (idOfList.length >= itemCount)?idOfList.length-itemCount:0;
			idOfList = Arrays.copyOfRange(idOfList,(int)(idOfList.length-itemCount), idOfList.length);
			List<POSHistory> list = new ArrayList<POSHistory>();
			if(idOfList.length == 1 && idOfList[0].equals("")){
				POSHistoryList = null;
			}else{
				for (int i=idOfList.length-1;i>=0;i--) {
					POSHistory a = new POSHistory(Long.parseLong(idOfList[i]));
					list.add(a);
				}
				for(POSHistory a : list)
					a.LoadData();
				POSHistoryList = list;	
			}
		}
		return POSHistoryList;
	}
	
	public List<POSHistory> getPOSHistoryList() {
		if ((POSHistoryList == null||POSHistoryList.size()== 0) && !(POSHistoryListString == null || POSHistoryListString.isEmpty())) {
			String[] idOfList = SplitList(POSHistoryListString);
			List<POSHistory> list = new ArrayList<POSHistory>();
			if(idOfList.length == 1 && idOfList[0].equals("")){
				POSHistoryList = null;
			}else{
				for (String id : idOfList) {
					POSHistory a = new POSHistory(Long.parseLong(id));
					list.add(a);
				}
				for(POSHistory a : list)
					a.LoadData();
				POSHistoryList = list;	
			}
		}
		return POSHistoryList;
	}

	public void setPOSHistoryList(List<POSHistory> POSHistoryList) {
		this.POSHistoryListString =CombineList(POSHistoryList);
		this.POSHistoryList = POSHistoryList;
	}
	
	public String getPOSHistoryListString(){
		return POSHistoryListString;
	}

	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public long getDisplayFolioId() {
		return displayFolioId;
	}

	public void setDisplayFolioId(long displayFolioId) {
		this.displayFolioId = displayFolioId;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.POSGuestTable);
		else
			item = new Entity(this.key);

		item.setProperty("hotelId", this.hotelId);
		item.setProperty("displayFolioId", this.displayFolioId);
		item.setProperty("status", this.status);
		item.setProperty("table", this.tableString);
		item.setProperty("guest", this.guestString);
		item.setProperty("guestAmount", this.guestAmount);
		item.setProperty("arrive", this.arrive);
		item.setProperty("nightAuditDate", this.nightAuditDate);
		item.setProperty("back", this.back);
		item.setProperty("POSorderList", this.POSorderListString);
		item.setProperty("note", this.note);
		item.setProperty("isClosed", this.isClosed);
		item.setProperty("guestStatus", this.guestStatus);
		item.setProperty("POSHistoryList", this.POSHistoryListString);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.displayFolioId = (Long) entity.getProperty("displayFolioId");
		this.tableString = (String) entity.getProperty("table");
		this.status = (Boolean) entity.getProperty("status");
		this.guestAmount = (String) entity.getProperty("guestAmount");
		this.guestString = (String) entity.getProperty("guest");
		this.arrive = (Date) entity.getProperty("arrive");
		this.nightAuditDate = (Date) entity.getProperty("nightAuditDate");
		this.back = (Date) entity.getProperty("back");
		this.POSorderListString=(String) entity.getProperty("POSorderList");
		this.note=(String) entity.getProperty("note");
		this.isClosed = (Boolean) entity.getProperty("isClosed");
		this.guestStatus = (String) entity.getProperty("guestStatus");
		this.POSHistoryListString = (String) entity.getProperty("POSHistoryList");
		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.POSGuestTable, this.id);
			setEntity(entity);
		}
	}
	
	public void addPOSHistory(POSHistory posHistory) {
		if(this.POSHistoryList == null)
			this.POSHistoryList = new ArrayList<POSHistory>();
		else
			getPOSHistoryList();
		if(this.POSHistoryListString!=null && this.POSHistoryListString.length() > 0)
			this.POSHistoryListString += "," + posHistory.getId();
		else
			this.POSHistoryListString = Long.toString(posHistory.getId());
		this.POSHistoryList = null;
	}
}
